home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / overview / dtscpluslibrary / sources / dtscpluslibrary.cp < prev    next >
Encoding:
Text File  |  2000-06-23  |  5.3 KB  |  197 lines

  1. /*
  2.     File:        DTSCPlusLibrary.cp
  3.  
  4.     Contains:    DTSCPlusLibrary.h contains all the global data and definitions needed when building
  5.                   more DTS C++ libraries. Every class should have this file included.
  6.                  DTSCPlusLibrary.cp contains functions that we could not inline inside the header (sigh).
  7.  
  8.     Written by:     
  9.  
  10.     Copyright:    Copyright Â© 1992-1999 by Apple Computer, Inc., All Rights Reserved.
  11.  
  12.                 You may incorporate this Apple sample source code into your program(s) without
  13.                 restriction. This Apple sample source code has been provided "AS IS" and the
  14.                 responsibility for its operation is yours. You are not permitted to redistribute
  15.                 this Apple sample source code as "Apple sample source code" after having made
  16.                 changes. If you're going to re-distribute the source, we require that you make
  17.                 it clear in the source that the code was descended from Apple sample source
  18.                 code, but that you've made changes.
  19.  
  20.     Change History (most recent first):
  21.                 8/18/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  22.                 
  23.  
  24. */
  25. #ifndef _DTSCPLUSLIBRARY_
  26. #include "DTSCPlusLibrary.h"
  27. #endif
  28. #include <TextUtils.h>
  29. #include <Sound.h>
  30. // _________________________________________________________________________________________________________ //
  31. // STRING FUNCTIONS
  32.  
  33. // return the len of a C string
  34. short clen(char *cptr)
  35. {
  36.     short    i;
  37.  
  38.     for (i = 0; cptr[i]; ++i)
  39.         ;
  40.     return(i);
  41. }
  42.  
  43.  
  44. // Convert a c-string to a pascal-string
  45.  
  46. void c2p(char *cptr)
  47. {
  48.     c2pstr(cptr);
  49.     /*char    len;
  50.     Str255 myStr;
  51.     ::BlockMove(cptr, &myStr[1], len = clen(cptr));
  52.     cptr[0] = len;*/
  53. }
  54.  
  55.  
  56. // Convert a pascal-string to a c-string
  57. void p2c(StringPtr cptr)
  58. {
  59.     char    len;
  60.  
  61.     ::BlockMove(cptr + 1, cptr, len = *cptr);
  62.     cptr[len] = 0;
  63. }
  64.  
  65.  
  66. // Copy two Pascal strings
  67. void Pstrcpy(StringPtr d, StringPtr s)
  68. {
  69.     short    i;
  70.  
  71.     i = *s;
  72.     do {
  73.         d[i] = s[i];
  74.     } while (i--);
  75. }
  76.  
  77.  
  78. // Concatenate two Pascal strings.
  79. void ConcatPStrings(Str255 first,
  80.                            Str255 second)
  81. {
  82.     short charsToCopy;
  83.  
  84.     // Truncate if concatenated string would be longer than 255 chars
  85.     charsToCopy = Min(second[0], 255 - first[0]);
  86.     ::BlockMove(second + 1, first + first[0] + 1, (long)charsToCopy);
  87.     first[0] += charsToCopy;
  88. }
  89.  
  90. // Compare two Pascal Strings.
  91. Boolean CompareStr255(const Str255 *first, const Str255 *second)
  92. {
  93.     short state = ::RelString(*first, *second, true, true);            // call toolbox comparison routine
  94.     if(state == sortsEqual)
  95.         return true;
  96.     else
  97.         return false;
  98. }
  99.  
  100. //    vxdebugstr that also prints out by default __FILE__ and __LINE__ information
  101. void vxdebugstr(char* format,...) 
  102. {
  103.     char buff[255];
  104.     char final[255];
  105.  
  106.     va_list arglist;
  107.     va_start(arglist,format);
  108.     vsprintf(buff,format,arglist);
  109.     va_end(arglist);
  110.  
  111. #ifdef FILELINEINFO    
  112.     strcat(final,fileNameArray);
  113. #ifdef TESTLEVEL2
  114.     strcat(final, " ;");                            // MacsBug does not like embedded ;s
  115. #endif
  116.     strcat(final, lineNumberArray);
  117. #endif
  118.  
  119.     strcat(final, buff);
  120.     c2p(final);
  121.     
  122.     // Print out the message, based on the testing level
  123. #ifdef TESTLEVEL1
  124.     AlertUser((StringPtr)final);
  125. #endif
  126. #ifdef TESTLEVEL2
  127.     ::SysBreakStr((StringPtr)final);                        // for high level debuggers
  128. #endif
  129. #ifdef TESTLEVEL3
  130.     ::DebugStr((StringPtr)final);
  131. #endif
  132. }
  133.  
  134.  
  135. // ErrorWindow (no resources)
  136. // Here's an example of how to create a window with error information, without using Alerts or 
  137. // any other resource-bound constructs:
  138. // Pre-defined messages needed in the final box.
  139. const char* kLabelMessage = "Serious Problem:";
  140. const char* kInformMessage = "Click with the mouse or hit a key to terminate the application…";
  141.  
  142. void ErrorWindow(char* message)
  143. // Present a nice error window with a defined message.
  144. {
  145.     Rect windowRect, textRect, labelRect, informRect;
  146.     WindowPtr win;
  147.     
  148.     // Define a window size and create a window
  149.     ::SetRect(&windowRect, 90, 70, 400, 200);
  150.     win = ::NewWindow(NULL, &windowRect, "\p", true, dBoxProc, (WindowPtr)-1, false, 0L);
  151.         
  152.         if(win != NULL)
  153.         {
  154.             ::SetPort(win);            // switch to the right port
  155.             ::InitCursor();            // make sure the cursor is normal
  156.             // First draw the label:
  157.             ::SetRect(&labelRect, 20, 5, 300, 18);
  158.             ::TextFace(bold);
  159.             ::TETextBox(kLabelMessage, strlen(kLabelMessage), &labelRect, teJustLeft);
  160.             // Then draw the message:
  161.             ::SetRect(&textRect, 20, 30, 300, 280);        // define a text rect
  162.             ::TextSize(9);
  163.             ::TETextBox(message, strlen(message), &textRect, teJustLeft);
  164.     
  165.             // Draw the inform line at the bottom of the box
  166.             ::SetRect(&informRect,10, 115, 300, 280);
  167.             ::TextFace(normal);    
  168.             ::TETextBox(kInformMessage, strlen(kInformMessage), &informRect, teJustLeft);
  169.                 
  170.             // now, spin around in WaitNextEvent until user hits a key click or hits the mouse
  171.             EventRecord anEvent;
  172.             while(!::WaitNextEvent(keyDownMask+mDownMask, &anEvent, 20L, NULL))
  173.                 ;
  174.             
  175.             // OK, we got the needed event, dispose the window.
  176.             ::DisposeWindow(win);
  177.         }    // Problems, we couldn't create a window, so all we could do is to beep for the end user.
  178.         else
  179.             ::SysBeep(1);
  180.         
  181.         ::ExitToShell();    // this is a serious problem, so we need to exit once and for all
  182. }
  183.  
  184. /* example of use: 
  185. ErrorWindow("Joe Montana can't play this Sunday: Error -9999");
  186. */
  187.  
  188. // _________________________________________________________________________________________________________ //
  189.  
  190. /*    Change History (most recent last):
  191.   No    Init.    Date        Comment
  192.   1        khs        9/10/92        New file
  193.   2        khs        11/26/92    Added ASSERT macros and testing levels
  194.   3        khs        1/14/93        Cleanup
  195. */
  196.  
  197.